//
//  KWebViewController.m
//  NeiHan
//
//  Created by Kevin on 2017/3/20.
//  Copyright © 2017年 柯平. All rights reserved.
//

#import "KWebViewController.h"
#import <WebKit/WebKit.h>
#import "ZMZWebContentView.h"

@interface KWebViewController ()<UIWebViewDelegate,WKUIDelegate,WKNavigationDelegate>

@property(nonatomic,weak)ZMZWebContentView *webBaseView;
@property (nonatomic, strong) UIProgressView *progressView;


@end

@implementation KWebViewController

-(void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
}

- (ZMZWebContentView *)webBaseView {
    if (!_webBaseView) {
        ZMZWebContentView *webView = [[ZMZWebContentView alloc] initWithFrame:self.view.bounds];
        webView.userInteractionEnabled = YES;
        webView.backgroundColor = kWhiteColor;
        WEAKSELF
        [webView setShouldStartLoadBlock:^BOOL(WKWebView *webView, NSURLRequest *request) {
            [weakSelf showLoading];
            return YES;
        }];
        [webView setFinshLoadBlock:^(WKWebView *webView) {
            [weakSelf hideHUD];
        }];
        [webView setLoadErrorBlock:^(WKWebView *webView, NSError *error) {
            [weakSelf hideHUD];
            [weakSelf showError:error];
        }];
        webView.propressBlock = ^(CGFloat propress) {
            weakSelf.progressView.progress = propress;
            if (propress == 1.0) {
                [UIView animateWithDuration:0.25f delay:0.3f options:UIViewAnimationOptionCurveEaseOut animations:^{
                    weakSelf.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.4f);
                } completion:^(BOOL finished) {
                    weakSelf.progressView.hidden = YES;
                }];
            } else {
                weakSelf.progressView.hidden = NO;
            }
        };
        [self.view addSubview:webView];
        _webBaseView = webView;
        
        self.progressView = [[UIProgressView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 2)];
        self.progressView.backgroundColor = [UIColor blueColor];
        //设置进度条的高度，下面这句代码表示进度条的宽度变为原来的1倍，高度变为原来的1.5倍.
        self.progressView.transform = CGAffineTransformMakeScale(1.0f, 1.5f);
        [self.view addSubview:self.progressView];
    }
    return _webBaseView;
}

- (void)setRequestUrl:(NSString *)requestUrl {
    _requestUrl = requestUrl;
    [self.webBaseView loadRequestWithURLString:requestUrl];
}

-(void)dealloc
{
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
